<?php

/**
 * This is the model class for table "{{account_payable}}".
 *
 * The followings are the available columns in table '{{account_payable}}':
 * @property integer $id
 * @property integer $projectId
 * @property string $token
 * @property string $payDate
 * @property string $summary
 * @property integer $merchantId
 * @property string $payee
 * @property string $ticketPayee
 * @property integer $type
 * @property string $price
 * @property string $note
 * @property integer $altMode
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property Merchant $merchant
 * @property User $createUser
 * @property Project $project
 * @property User $updateUser
 */
class AccountPayable extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return AccountPayable the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	private static $_types = array(
		'1' => '发票',
		'2' => '收据',
		'3' => '白条',
		'10' => '其它'
	);

	public static function getTypes() {
		return self::$_types;
	}

	public static function getPopularSummaries() {
		$id = __CLASS__ . '.summary.popular';
		$value = Yii::app()->cache->get($id);
		if ($value === false) {
			$criteria = new CDbCriteria();
			$criteria->distinct = true;
			$criteria->select = 'summary';
			$criteria->order = 'count(*) desc';
			$criteria->limit = 12;
			$criteria->group = 'summary';
			$models = AccountPayable::model()->findAll($criteria);
			$value = array();
			foreach ($models as $model) {
				$value[] = $model->summary;
			}
			Yii::app()->cache->set($id, $value, YII_DEBUG ? 1 : 60);
		}
		return $value;
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{account_payable}}';
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'projectId, token, payDate, summary, merchantId, payee, ticketPayee, type, price, note, altMode'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'projectId, payDate, price, type',
				'required'
			),
			array(
				'projectId, merchantId, type, altMode',
				'numerical',
				'integerOnly' => true
			),
			array(
				'token, summary, payee, ticketPayee, note',
				'length',
				'max' => 255
			),
			array(
				'merchantId+payee',
				'components.db.RequireOneValidator',
				'errorAttributes' => array(
					'payee'
				),
			),
			array(
				'price',
				'length',
				'max' => 16
			),
			array(
				'price',
				'numerical',
			),
			array(
				'payDate',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, projectId, token, payDate, summary, merchantId, payee, type, price, note, altMode, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'merchant' => array(
				self::BELONGS_TO,
				'Merchant',
				'merchantId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
			'project' => array(
				self::BELONGS_TO,
				'Project',
				'projectId'
			),
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'projectId' => '项目',
			'token' => '凭证号',
			'payDate' => '付款日期',
			'summary' => '摘要',
			'merchantId' => '商家',
			'payee' => '付款对象',
			'ticketPayee' => '票据付款对象',
			'type' => '票据类型',
			'price' => '付款额',
			'note' => '备注',
			'altMode' => '公司模式',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	public function scopes() {
		return array(
			'altMode' => array(
				'condition' => $this->getTableAlias() . '.altMode=0',
			),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('projectId', $this->projectId);
		$criteria->compare('token', $this->token, true);
		$criteria->compare('payDate', $this->payDate, true);
		$criteria->compare('summary', $this->summary, true);
		$criteria->compare('merchantId', $this->merchantId);
		$criteria->compare('payee', $this->payee, true);
		$criteria->compare('ticketPayee', $this->ticketPayee, true);
		$criteria->compare('type', $this->type);
		$criteria->compare('price', $this->price, true);
		$criteria->compare($this->getTableAlias() . '.note', $this->note, true);
		$criteria->compare('altMode', $this->altMode);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);
		$criteria->with = array(
			'project',
			'merchant'
		);
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public function getDisplayType() {
		if (isset(self::$_types[$this->type])) {
			return self::$_types[$this->type];
		}
		return null;
	}
}
